layui.use(['form', 'table', 'layer', 'xmSelect'], function () {
    var $ = layui.jquery,
        form = layui.form,
        table = layui.table,
        layer = layui.layer,
        xmSelect = layui.xmSelect;

    loadData(table);

    // 监听搜索操作
    form.on('submit(data-search-btn-article)', function (data) {
        //执行搜索重载
        table.reload('blogArticleList', {
            page: {curr: 1},//重新从第 1 页开始
            where: data.field
        });

        return false;
    });

    // 状态 - 开关操作
    form.on('switch(statusInput)', function(obj){
        var ids = this.attributes.data.value;
        var status = obj.elem.checked ? "1":"0";
        const param = {'id':parseInt(ids), "articleStatus":status};
        JSONAuthAjax(dl_coder_url.update_article_status[0], dl_coder_url.update_article_status[1], param);
    });

    /**
     * toolbar监听事件
     */
    table.on('toolbar(blogArticleListFilter)', function (obj) {
        if (obj.event === 'add') {  // 监听添加操作
            addArticle();
        }

        if (obj.event === 'edit') {  // 监听添加操作
            init_update_article(table);
        }

        if (obj.event === 'delete') {  // 监听删除操作
            deleteArticle(table);
        }
    });

    //监听表格复选框选择
    table.on('checkbox(blogArticleListFilter)', function (obj) {
        var cacheElement = table.cache['blogArticleList'];
        tableCheckBoxOnly(obj,cacheElement);
    });



});

function deleteArticle(table) {
    var selectdata = table.checkStatus('blogArticleList').data;
    if (selectdata.length === 0) {
        lay_tishi("选择一条数据");
        return false;
    }
    if (selectdata.length > 1) {
        lay_tishi("有多条数据");
        return false;
    }
    layer.confirm('确认删除', function (index) {
        const param = {'id':selectdata[0].id};
        var resultData = JSONAuthAjax(dl_coder_url.delete_article[0], dl_coder_url.delete_article[1], param);

        if (resultData.code === '200') {
            loadData(table);
            layer.close(index);
        }
    });
}

function addArticle() {
    layer.open({
        title: '添加文章',
        type: 2,
        maxmin:true,
        area: ['100%', '100%'],
        content: '../../html/blog/article-edit.html',
        success: function(layers, index){
            layer.full(index);
        },
        min: function(layers, index){
            layer.min(index);
            $("#layui-layer-shade"+index).show();
            return false;
        }
    });

}

function init_update_article(table) {
    var selectSortdata = table.checkStatus('blogArticleList').data;
    if (selectSortdata === null || selectSortdata.length === 0) {
        lay_tishi("选择一条数据");
        return false;
    }
    if (selectSortdata.length > 1) {
        lay_tishi("有多条数据");
        return false;
    }

    var id = selectSortdata[0].id;
    layer.open({
        title: '修改文章',
        type: 2,
        shade: 0.5,
        maxmin:true,
        resize: true,
        area: ['100%', '100%'],
        content: '../../html/blog/article-edit.html?id='+id,
        success: function(layers, index){
            layer.full(index);
        },
        min: function(layers, index){
            layer.min(index);
            $("#layui-layer-shade"+index).show();
            return false;
        },
        cancel: function(index, layero){
            layer.close(index);
            return true;
        }
    });
}


function loadData(table, where) {
    table.render({
        elem: '#blogArticleList',
        url: dl_coder_url.article_page[0],
        toolbar: '#toolbarBlogArticle',
        method: dl_coder_url.article_page[1],
        where: where,
        contentType: 'application/json',
        dataType: 'json',
        request: {
            pageName: 'pageNo', //页码的参数名称，默认：page
            limitName: 'pageSize', //每页数据量的参数名，默认：limit
        },
        headers: headerMap,
        defaultToolbar: ['filter', 'exports', 'print', {
            title: '提示',
            layEvent: 'LAYTABLE_TIPS',
            icon: 'layui-icon-tips'
        }],
        parseData: function (res) { //res 即为原始返回的数据
            return {
                "code": res.code, //解析接口状态
                "msg": res.msg, //解析提示文本
                "total": res.data.total, //解析数据长度
                "data": res.data.records, //解析数据列表
                "count": res.data.pages, //解析数据长度
            }
        },
        response: {
            statusName: 'code', //规定数据状态的字段名称，默认：code
            statusCode: 200,//规定成功的状态码，默认：0
            msgName: 'msg', //规定状态信息的字段名称，默认：msg
            countName: 'total', //规定数据总数的字段名称，默认：count
            dataName: 'data', //规定数据列表的字段名称，默认：data
        },
        cols: [[
            {type: "checkbox"},
            {field: 'id',align: 'center', width: 80, title: 'ID', sort: true},
            {field: 'articleTitle',align: 'center', title: '文章标题'},
            {field: 'articleEye',align: 'center', width: 200, title: '文章查看人数'},
            {field: 'articleSort',align: 'center',  width: 200, title: '文章分类'},
            {field: 'articleImgDesc',align: 'center', width: 200, title: '图片描述'},
            {field: 'articleImg',align: 'center',  width: 200, title: '图片',templet: "#imgArticle"},
            {field: 'articleTag',align: 'center', width: 200, title: '文章标签'},
            {field: 'articleTagId',align: 'center', width: 200, title: '文章标签Id', hide:true},
            {field: 'createTime',align: 'center', width: 200, title: '添加时间'},
            {field: 'articleStatus', width: 200, title: '操作',align: 'center',templet: "#contrArticleStatus"}
        ]],
        limits: [10, 15, 20, 25, 50, 100],
        limit: 15,
        page: true,
        skin: 'line',
        even: true,
        size: 'lg',
        lineStyle: 'height: 95px;',
        done: function () {
            setAuthority();
        }
    });
}
